package com.df.bpm.utils.workCardDemo;

import com.df.bpm.utils.workCardDemo.demoEntity.DefaultResponse;
import com.df.bpm.utils.workCardDemo.demoEntity.DeviceResponse;
import com.df.bpm.utils.workCardDemo.demoEntity.TemplateData;
import com.google.gson.Gson;
import okhttp3.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class demoAPI {

    public static void main(String[] args) throws IOException {
        try {
            String server="http://192.168.0.34:4000";
            String token=getToken(server);
//            List<Map<String,String>> devices=getDevices(server,token);
//            DefaultResponse res=setDeviceRGB(server,token,"D4:3D:39:7C:F9:E6",0,0,0);//不是登录账户注册的卡不能控制
            TemplateData templateData=new TemplateData();
            templateData.tid="66be2830c3fdfeb9c65cb9c6";
            templateData.tname="abc";
            templateData.data=new HashMap<>();
            templateData.data.put("number", "380XXX");
            templateData.data.put("name", "曾一峰");
            templateData.data.put("msg", "努力搬砖");
            DefaultResponse res=setDeviceTemplate(server,token,"D4:3D:39:7C:F1:4A",templateData);
            System.out.println(res);
//            test();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

//    private static void test() throws Exception {
//        OkHttpClient client = new OkHttpClient().newBuilder()
//                .build();
//        MediaType mediaType = MediaType.parse("application/json");
//        String iData="[{\"location\":\"1-20\",\"cost\":11,\"scrap\":0},{\"location\":\"1-21\",\"cost\":21,\"scrap\":0},{\"location\":\"1-22\",\"cost\":21,\"scrap\":0},{\"location\":\"1-23\",\"cost\":21,\"scrap\":0},{\"location\":\"1-25\",\"cost\":21,\"scrap\":0},{\"location\":\"1-27\",\"cost\":21,\"scrap\":0},{\"location\":\"2-20\",\"cost\":21,\"scrap\":0},{\"location\":\"2-21\",\"cost\":21,\"scrap\":0},{\"location\":\"2-22\",\"cost\":21,\"scrap\":0},{\"location\":\"2-23\",\"cost\":21,\"scrap\":0},{\"location\":\"2-24\",\"cost\":21,\"scrap\":0},{\"location\":\"2-25\",\"cost\":21,\"scrap\":0},{\"location\":\"2-26\",\"cost\":10,\"scrap\":0},{\"location\":\"3-20\",\"cost\":21,\"scrap\":0},{\"location\":\"3-21\",\"cost\":21,\"scrap\":0},{\"location\":\"3-22\",\"cost\":21,\"scrap\":0},{\"location\":\"3-23\",\"cost\":42,\"scrap\":0},{\"location\":\"3-25\",\"cost\":21,\"scrap\":0},{\"location\":\"4-23\",\"cost\":2,\"scrap\":2},{\"location\":\"4-23\",\"cost\":8,\"scrap\":3},{\"location\":\"4-23\",\"cost\":2,\"scrap\":2},{\"location\":\"4-23\",\"cost\":16,\"scrap\":0}]";
//        RequestBody body = RequestBody.create(mediaType, new Gson().toJson(iData));
//        String url="http://192.168.0.40:58064/API/setPCBmountOver?2409060102921";
//        Request request = new Request.Builder()
//                .url(url)
//                .method("POST", body)
////                .addHeader("Authorization", "Bearer "+token)
//                .addHeader("Content-Type", "application/json")
//                .build();
//        Response response = client.newCall(request).execute();
//        byte result[]=response.body().bytes();
//        String msg=new String(result);
//        System.out.println(msg);
//    }

    private static DefaultResponse setDeviceTemplate(String server, String token, String mac, TemplateData templateData) throws Exception {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, new Gson().toJson(templateData));
        String url=server+"/user/api/mqtt/publish/"+mac+"/template/"+templateData.tid;
        Request request = new Request.Builder()
                .url(url)
                .method("POST", body)
                .addHeader("Authorization", "Bearer "+token)
                                .addHeader("Content-Type", "application/json")
                                .build();
        Response response = client.newCall(request).execute();
        byte result[]=response.body().bytes();
        String msg=new String(result);
        DefaultResponse res=new Gson().fromJson(msg, DefaultResponse.class);
        return res;
    }

    private static DefaultResponse setDeviceRGB(String server, String token, String mac,int R,int G,int B) throws Exception {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, "{\r\n \"red\": "+R
                +",\r\n \"green\":"+G+",\r\n \"blue\": "+B+"\r\n}");
        String url=server+"/user/api/mqtt/publish/"+mac+"/led";
        Request request = new Request.Builder()
                .url(url)
                .method("POST", body)
                .addHeader("Authorization", "Bearer "+token)
                                .addHeader("Content-Type", "application/json")
                                .build();
        Response response = client.newCall(request).execute();
        byte result[]=response.body().bytes();
        String msg=new String(result);
        DefaultResponse res=new Gson().fromJson(msg, DefaultResponse.class);
        return res;
    }

    private static List<Map<String, String>> getDevices(String server, String token) throws Exception {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        Request request = new Request.Builder()
                .url(server+"/user/api/rest/devices?query=mac,ip")
                .method("GET", null)
                .addHeader("Authorization", "Bearer "+token)
                                .build();
        Response response = client.newCall(request).execute();
        byte result[]=response.body().bytes();
        String msg=new String(result);
        DeviceResponse res=new Gson().fromJson(msg, DeviceResponse.class);
        return res.data.items;
    }

    private static String getToken(String server) throws Exception {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, "{\r\n \"username\": \"admin\",\r\n\"password\": \"admin123\"\r\n}");
        Request request = new Request.Builder()
                .url(server+"/user/api/login")
                .method("POST", body)
                .addHeader("Content-Type", "application/json")
                .build();
        Response response = client.newCall(request).execute();
        byte result[]=response.body().bytes();
        String msg=new String(result);
        DefaultResponse res=new Gson().fromJson(msg, DefaultResponse.class);
        return res.data.get("token");
    }


}
